# -*- coding: utf-8 -*-
# @Time    : 2019-12-18 10:22
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : R12_EC_CurrOweCheck_WHB_2D.py
# @Software: PyCharm


'''eof
name:企业当前欠息天数
code:R12_EC_CurrOweCheck_WHB_2D
tableName:
columnName:
groups:弱规则>企业征信校验
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import re
import datetime
import pandas as pd
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return ""
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def Formate(time):
    import re
    import datetime
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date


def R12_EC_CurrOweCheck_WHB_2D():
    factor = []
    data = EC_DSJ_2D['data']
    # 借贷账户信息
    EDA = getTable(data, 'EDA', 1)
    if EDA is None:
        return u'缺失值'
    elif EDA == "":
        return 1
    else:
        # 欠息信息单元
        ED03 = getTable(EDA[0], 'ED03', 0)
        if ED03 is None or ED03 == "":
            return u'缺失值'
        elif ED03 == [{}]:
            return 1
        else:
            try:
                for ED03_Items in ED03:
                    # 余额变化日期(ED030R01)
                    ED030R01 = Formate(ED03_Items.get('ED030R01'))
                    # 欠息余额
                    ED030J01 = ED03_Items.get('ED030J01')
                    if ED030R01 is None or isfloat(ED030J01) == False:
                        factor.append(None)
                    else:
                        ED030R01_In = (datetime.datetime.now() - pd.tseries.offsets.DateOffset(days=60)).strftime(
                            '%Y%m%d')
                        if ED030R01 < ED030R01_In and float(ED030J01)>0:
                            factor.append(1)
                        else:
                            factor.append(0)
            except:
                factor.append(None)

    factorNotNone = filter(lambda x: x is not None, factor)
    if len(factorNotNone) == 0:
        return u'缺失值'
    elif sum(factorNotNone) > 0:
        return 0
    else:
        return 1

result = R12_EC_CurrOweCheck_WHB_2D()